﻿using Autofac;
using MgSoft.Component;
using MgSoft.Import.Exchange.ExWebApi.Db.Daos;
using MgSoft.Import.Exchange.ExWebApi.Db.Dtos;
using MgSoft.Import.Exchange.ExWebApi.Db.Services;
using System;
using System.Collections.Generic;
using System.Text;

namespace MgSoft.Import.Exchange.ExWebApi.Db
{
    public class ComponentExWebApiDb : MgComponent
    {
        ConfigDto configDto;

        public override void Register(object builder)
        {
            ContainerBuilder containerBuilder = builder as ContainerBuilder;
            if (containerBuilder == null) throw new ArgumentException("builder type must ContainerBuilder");

            containerBuilder.RegisterType<SyncDataDao>().As<ISyncDataDao>();
            containerBuilder.RegisterType<DbTask>().As<IDbTask>();
            containerBuilder.RegisterType<DbTaskService>().As<IDbTaskService>();

            containerBuilder.RegisterType<ComponentExWebApiDb>().OnActivating(p => p.ReplaceInstance(this));
            containerBuilder.RegisterType<ConfigDto>().OnActivating(p=>p.ReplaceInstance(configDto));

        }

        public void Config(Action<ConfigDto> cfg)
        {
            configDto = new ConfigDto();
            cfg.Invoke(configDto);
        }
    }
}
